<template>
  <view>
    <view class="goods-list">
      <view v-for="(item, i) in goodsList" :key="i" @click="gotoDetail(item)">
        <!-- 为 my-goods 组件动态绑定 goods 属性的值 -->
        <my-goods :goods="item"></my-goods>
      </view>
   
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
        queryObj: {
          // 查询关键词
          query:'',
          // 商品分类id
          cid:'',
          // 页码值
          pagenum: 1 ,
          // 每页显示多少条数据
          pagesize: 10
        },
        // 商品列表的数据
        goodsList:[],
        // 商品总数，用来实现分页
        total: 0,
        //节流阀
        isLoading:false
      }
    },
    onLoad(option) {
      // 将页面参数转存到 this.queryObj 对象中
      this.queryObj.query = option.query || '',
      this.queryObj.cid = option.cid || ''
      this.getGoodsList()
    },
    methods: {
     async getGoodsList(cb){
       this.isLoading = true
        const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
         // 只要数据请求完毕，就立即按需调用 cb 回调函数
          cb && cb()
        this.isLoading = false
        if(res.meta.status != 200) return uni.$showMsg()
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
       
      },
      gotoDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    },
    onReachBottom() {
      // 判断是否正在请求其它数据，如果是，则不发起额外的请求
      if(this.isLoading) return
      // 判断是否还有下一页数据
      if(this.queryObj.pagenum * this.queryObj.pagesize >=this.total) return uni.$showMsg('数据加载完毕！')
       // 让页码值自增 +1
       this.queryObj.pagenum +=1
       this.getGoodsList()
    },
    onPullDownRefresh() {
      // 重置关键数据
      this.queryObj.pagenum = 1
      this.total = 0
      this.isLoading = false
      this.goodsList = []
      // 重庆发起请求
      this.getGoodsList(() => uni.stopPullDownRefresh())
    },
   
  }
</script>

<style lang="scss">

</style>
